package tuio;

import java.awt.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import tuio.libTUIO.src.TUIO.TuioCursor;

/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class TuioControl {

	private HashMap<Integer, TCurAndTouchListener> tuioList = new HashMap<Integer, TCurAndTouchListener>();
	
	private TuioCursor previousTouch;
	
	public TuioControl(){
	}

	public ArrayList<Integer> getAllTouchIdInComponent(Component c) {
		Iterator<Integer> iter = tuioList.keySet().iterator();
		ArrayList<Integer> list = new ArrayList<Integer>();
		while(iter.hasNext()){
			Integer id = iter.next();
			I_TouchListener current = tuioList.get(id).getTouchListener();
			if(current==c)list.add(id);
		}
		return list;
	}
	
	public boolean componentAlreadyHasATouch(Component c){
		Iterator<Integer> iter = tuioList.keySet().iterator();
		while(iter.hasNext()){
			Integer id = iter.next();
			I_TouchListener current = tuioList.get(id).getTouchListener();
			if(current==c)return true;
		}
		return false;
	}
	

	/**
	 * @param tcur le curseur teste
	 * @return true si pression longue (1 sec) quasi immobile
	 */
	public boolean isLongPress(TuioCursor tcur) {
		if(tuioList.containsKey(tcur.getCursorID())){
			if(tcur.getTuioTime().subtract(tuioList.get(tcur.getCursorID()).gettCurAndPoint().gettCur().getStartTime()).getTotalMilliseconds()>1000){
				return true;
			}
		}
		return false;
	}

	public void setPreviousTouch(TuioCursor tuioCursor) {
		this.previousTouch = tuioCursor;
	}

	public TuioCursor getPreviousTouch() {
		return previousTouch;
	}

	public void setTuioList(HashMap<Integer, TCurAndTouchListener> tuioList) {
		this.tuioList = tuioList;
	}

	public HashMap<Integer, TCurAndTouchListener> getTuioList() {
		return tuioList;
	}
	
	
	public String getTuioListInfos(){
		Iterator<Integer> iter = tuioList.keySet().iterator();
		String res = "TuioList :";
		while(iter.hasNext()){
			Integer id = iter.next();
			res=res+"\n ID="+tuioList.get(id).gettCurAndPoint().gettCur().getCursorID()+" Component="+(((Component) tuioList.get(id).getTouchListener())).getName();
		}
		return res;
	}

	public int getNbOfTouchInComponent(Component c) {
		return getAllTouchIdInComponent(c).size();
	}


}
